define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lihua_vouchers/index' + location.search,
                    add_url: 'lihua_vouchers/add',
                    edit_url: 'lihua_vouchers/edit',
                    del_url: 'lihua_vouchers/del',
                    multi_url: 'lihua_vouchers/multi',
                    table: 'lihua_vouchers',
                }
            });

            var table = $("#table");

            $(".admin_do").click(function(v){
                var id = $(this).data("ids");
                console.log(id);
                // $("#opt_id").val(v);
            })

            $('#exampleModal').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget) // Button that triggered the modal
                var id = button.data('ids') // Extract info from data-* attributes
                // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
                // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
                var modal = $(this);
                modal.find('.modal-body #opt_id').val(id);

            })
            
            //点击递交表单
            $("#admin_do_sub").click(function(){
                let payload = {
                    "opt_name" : $("#opt_name").val(),
                    "opt_pwd" : $("#opt_pwd").val(),
                    "opt_id" : $("#opt_id").val(),
                }
                if(payload.opt_name.length<2){alert("请输入正确操作人员名称");return ;}
                if(payload.opt_name.opt_pwd<6){alert("请输入正确的操作密码");return ;}
                $.post("../api/user/vouchers_admin_do", {...payload}, function(res){
                    console.log(res);
                    $('#exampleModal').modal('hide');
                    $(".btn-refresh").click();
                    $("#opt_pwd").val("");
                });
            })

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'city_id', title: __('City_id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'sn', title: __('Sn')},
                        {field: 'start_date', title: __('Start_date'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'end_date', title: __('End_date'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: '使用类型', searchList: {"0":'不限',"1":"线上",'2':'线下'}, formatter: Table.api.formatter.normal},
                        {field: 'order_sn', title: __('Order_sn')},
                        {field: 'price', title: '面值'},
                        {field: 'remark', title: '备注', operate:'LIKE',},
                        {field: 'consignee', title: '经办人', operate:'LIKE',},
                        {field: 'section', title: '部门', operate:'LIKE',},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin_operate', title: "管理员处理", table: table, formatter: Controller.api.formatter.admin_operate},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                admin_operate: function (value, row, index) {
                    return '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-ids="'+row.id+'" style="padding: 0px 5px;">超级处理</button>';
                },
            },
        }
    };
    return Controller;
});